/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boot;

import boot.constant.BootPropertiesLoader;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.xml.XmlPage;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import persistence.file.FileManager;


/**
 *
 * @author Ricardo
 */
public class Boot {
    
   private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getRootLogger();
   BootPropertiesLoader properties = new BootPropertiesLoader(); 
   Set urlSet = new HashSet();
 
public void download() throws Exception{
    Collection<String> sourceColl = this.getSources();
    
    for (String source : sourceColl) {
       this.downloadPage(source);    
    }
    
}   
private void downloadPage( String url) throws Exception {
    log.info("Init dowload page URL:" + url);
    final WebClient webClient = new WebClient();
    
    SgmlPage page = null;
    String pageAsText = null;
    try{
       page = webClient.getPage(url);
       pageAsText = page.asText();
    }
    catch ( java.lang.ClassCastException e ){
       page = (XmlPage) webClient.getPage(url);
       pageAsText = page.asXml();
    }
    this.writeFile(pageAsText);
    webClient.closeAllWindows();
}

private void writeFile( String pageAsText ){
    int sequence = 0; 
    FileManager fileManager = new FileManager();
    String folder = properties.getProperty("file.source.path");
    String fileName = sequence+++".txt";
    log.info("Call writeFile");
    fileManager.writeFile(folder, fileName, pageAsText);
}

private Collection<String>getSources(){
    String propertyFonts = properties.getProperty("fonts");
    String[] fontsColl = propertyFonts.split(",");
    return Arrays.asList(fontsColl);
}


private boolean isDuplicated( String url ){
    boolean duplicated = false;
    if(!urlSet.add(url)){
        duplicated = true;
        log.info("URL Duplicate detected : " + url);
    }
    log.info("new URL found:" + url);
    return duplicated;
}

    
    
    
}
